
import hibernate.hibernateInterface.RoomSystemHibernateInterface;
import hibernate.roomSystem.Room;
import java.util.LinkedList;
import org.hibernate.Session;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.HtmlMacroComponent;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Label;
import org.zkoss.zul.Window;
import org.zkoss.zul.api.*;
import utilities.utilities_components.PlainList;
import utilities.event.ACSListener;

public class GuestSearchController extends Window{
	public void showPlainList()
	{
		PlainList pList = new PlainList();
		pList.setId("GUEST_RESULT_LIST");
		String[] header = {"姓名","房号","房型","性别","出生年月","证件类型","证件编码","来自","登记时间","团队","备注"};
		String[] footer = {"合计","0","人次"};
		pList.setHead(header);
		int [] para1 = {1,1,1} , para2 = {0,2,3} , para3 = {0,2},para4 = {0,1,2,3,4,8,9};
		pList.setFooter(footer, para1, para2, para3);
		pList.setSortable(para4);
		pList.setParent((Component)this);
		this.addEventListener("onNotify", new ACSListener());
	}
	
	public void ageBoxCheck()throws Exception
	{
		Intbox box1 = (Intbox)getFellow("ACS_LOWER_AGE_BOX");
		Intbox box2 = (Intbox)getFellow("ACS_HIGHER_AGE_BOX");
		if (box1.getValue() != null &&
			box2.getValue() != null &&
			box1.getValue() >= box2.getValue())
		{
			box2.setValue(0);
			throw new WrongValueException(box2,"年龄上限应大于下限");
		}
	}
	
	public void dateBoxCheck()throws Exception
	{
		Datebox box1 = (Datebox)getFellow("ACS_TIME_FROM_BOX");
		Datebox box2 = (Datebox)getFellow("ACS_TIME_TO_BOX");
		if (box1.getValue() != null &&
			box2.getValue() != null &&
			box1.getValue().compareTo(box2.getValue()) >= 0)
		{
			box2.setValue(null);
			throw new WrongValueException(box2,"起始日期应先于截止日期");
		}
	}
	
	public void fillComboBox()
	{
		String [] items = {"单人间","双人间","标准间","套间","三人间","四人间"};//array of roomType
		
		Session session = HibernateInitializer.getSession();
		LinkedList roomList = new LinkedList();
		try {
			roomList.addAll(RoomSystemHibernateInterface.getAllRooms(session));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		String[] items2 = new String[roomList.size()];//array of roomNumber
		for (int i = 0; i < roomList.size();i++)
		{
			items2[i] = ((Room)roomList.get(i)).getRoomNumber();
		}
		
		Combobox resBox = (Combobox)getFellow("CS_SEARCH_RESTRICTION_BOX");
		Combobox roomResBox = (Combobox)getFellow("CS_ROOM_RESTRICTION_BOX");
		
		if (resBox.getSelectedIndex() == 1)
		{
			int i;
			for(i = 0;i < roomResBox.getItemCount();i++)
				roomResBox.removeItemAtApi(i);
			for (i = 0;i <items.length;i++)
				roomResBox.appendItemApi(items[i]);	
		}
		else
		{
			int i;
			for(i = 0;i < roomResBox.getItemCount();i++)
				roomResBox.removeItemAtApi(i);
			for (i = 0; i < items2.length;i++)
				roomResBox.appendItemApi(items2[i]);
		}
	}
	
	public void onSearch()
	{
		this.getPage().setAttribute("list", (PlainList)getFirstChild().getFellow("GUEST_RESULT_LIST"));
		this.getPage().setAttribute("name", ((Textbox)getFirstChild().getFellow("ACS_NAME_BOX")).getValue());
		this.getPage().setAttribute("ageCheck", ((Checkbox)getFirstChild().getFellow("ACS_AGE_CHECK")).isChecked());
		this.getPage().setAttribute("low_age", ((Intbox)getFirstChild().getFellow("ACS_LOWER_AGE_BOX")).getValue());
		this.getPage().setAttribute("high_age", ((Intbox)getFirstChild().getFellow("ACS_HIGHER_AGE_BOX")).getValue());
		this.getPage().setAttribute("timeFrom", ((Datebox)getFirstChild().getFellow("ACS_TIME_FROM_BOX")).getValue());
		this.getPage().setAttribute("timeTo", ((Datebox)getFirstChild().getFellow("ACS_TIME_TO_BOX")).getValue());
		this.getPage().setAttribute("sex", 2);
		if (((Combobox)getFirstChild().getFellow("ACS_SEX_BOX")).getSelectedIndex() == 0)
			this.getPage().setAttribute("sex", 0);
		else if (((Combobox)getFirstChild().getFellow("ACS_SEX_BOX")).getSelectedIndex() == 1)
			this.getPage().setAttribute("sex", 1);
		this.getPage().setAttribute("searchRes", ((Combobox)getFirstChild().getFellow("ACS_SEARCH_RESTRICTION_BOX")).getSelectedItemApi());
		this.getPage().setAttribute("roomRes", ((Combobox)getFirstChild().getFellow("ACS_ROOM_RESTRICTION_BOX")).getSelectedItemApi());
		Events.sendEvent(new Event("onNotify",this));
	}
}
